 /* 
 * File:   cAbstractAnimatedObject.cpp
 * Author: josep
 * 
 * Created on 4 / gener / 2011, 18:22
 */

#include "cAbstractAnimatedObject.h"

cAbstractAnimatedObject::cAbstractAnimatedObject() {
}

cAbstractAnimatedObject::cAbstractAnimatedObject(const cAbstractAnimatedObject& orig) {
    this->frameRate=orig.frameRate;
    this->frameSeq=orig.frameSeq;
    this->frameState=orig.frameState;
    this->nFrameStates=orig.nFrameStates;
    this->nTilesHor=orig.nTilesHor;
    this->nTilesVer=orig.nTilesVer;
    this->nextTime=orig.nextTime;
    this->tileHorRatio=orig.tileHorRatio;
    this->tileVerRatio=orig.tileVerRatio;
    this->timePerFrame = orig.timePerFrame;
    this->imageSequencesVector=vector< vector< vector<int> > >(orig.imageSequencesVector);
}

cAbstractAnimatedObject::~cAbstractAnimatedObject() {
}

void cAbstractAnimatedObject::Draw(int PosX, int PosY, cTexture* Texture,
                                        int TextureWidth,  int TextureHeight){
    float x, y;

    if(frameSeq >= imageSequencesVector[GetFrameState()].size()){
        frameSeq = 0;
    }
    x=(float)imageSequencesVector[GetFrameState()][frameSeq][X] * tileHorRatio;
    y=(float)imageSequencesVector[GetFrameState()][frameSeq][Y] * tileVerRatio;


    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, Texture->GetID());
    glBegin(GL_QUADS);
        glTexCoord2f(x, y);
        glVertex2i(PosX , PosY + TextureHeight*tileVerRatio);

        glTexCoord2f(x+tileHorRatio,y);
        glVertex2i(PosX + TextureWidth*tileHorRatio,
                        PosY + TextureHeight*tileVerRatio);

        glTexCoord2f(x+tileHorRatio, y+tileVerRatio);
        glVertex2i(PosX + TextureWidth*tileHorRatio,PosY);

        glTexCoord2f(x, y+tileVerRatio);
        glVertex2i(PosX, PosY);
    glEnd();
    glDisable(GL_TEXTURE_2D);

    long mtime = clock() / CLOCKS_PER_MILIS;

    if(mtime>nextTime){
        nextTime=mtime+timePerFrame;
	frameSeq++;
    }
}

int cAbstractAnimatedObject::GetFrameState()  {
    return frameState;
}

void cAbstractAnimatedObject::SetFrameState(int frameState) {
    this->frameState = frameState;
}
vector<vector<vector<int> > >* cAbstractAnimatedObject::GetImageSequencesVector(){
    return &imageSequencesVector;
}
void cAbstractAnimatedObject::SetTimePerFrame(long timePerFrame) {
    this->timePerFrame = timePerFrame;
}
void cAbstractAnimatedObject::SetNextTime(long nextTime) {
    this->nextTime = nextTime;
}
void cAbstractAnimatedObject::SetNFrameStates(int nFrameStates) {
    this->nFrameStates = nFrameStates;
}
void cAbstractAnimatedObject::SetNTilesVer(int nTilesVer) {
    this->nTilesVer = nTilesVer;
    tileVerRatio = 1.0f/(float)nTilesVer;
}
void cAbstractAnimatedObject::SetNTilesHor(int nTilesHor) {
    this->nTilesHor = nTilesHor;
    tileHorRatio = 1.0f/(float)nTilesHor;
}
void cAbstractAnimatedObject::SetFrameSeq(int frameSeq) {
    this->frameSeq = frameSeq;
}
void cAbstractAnimatedObject::SetFrameRate(int frameRate) {
    this->frameRate = frameRate;
    timePerFrame = 1000/frameRate;
    nextTime = clock()/CLOCKS_PER_MILIS + timePerFrame;
}
int cAbstractAnimatedObject::GetFrameSeq() const {
    return frameSeq;
}

